package org.lq.ht.view.room;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import org.lq.ht.entity.Customer;
import org.lq.ht.entity.Dictionary;
import org.lq.ht.entity.TypeID;
import org.lq.ht.service.impl.CustomerServiceImpl;
import org.lq.ht.service.impl.DictionaryServiceImpl;
import org.lq.ht.util.CastUtils;
import org.lq.ht.util.ShowMessage;

import com.eltima.components.ui.DatePicker;
import java.awt.Font;

/**客人信息添加视图
 * @author 黄明慧
 *
 * @package org.lq.ht.view.room
 * @date 2020年8月21日下午11:29:46
 */
public class AddCustomer extends JFrame {

	private JPanel contentPane;
	private JTextField txt_name;
	private JTextField text_charge;
	private JTextField txt_phone;
	private JTextField txt_note;
	private JTextField txt_address;
	private JTextField text_idCard;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private final ButtonGroup buttonGroup_1 = new ButtonGroup();
	private final ButtonGroup buttonGroup_2 = new ButtonGroup();
	private JComboBox vip_level;
	private DatePicker txt_leaveTime = new DatePicker();
	private JRadioButton radio_boy;
	private JRadioButton radio_gril;
	private JComboBox cb_roomType;
	private MyTwoRoomTable trt;
	private DatePicker dp1;
	private DatePicker dp2;
	CustomerServiceImpl customerService = new CustomerServiceImpl();
	public JComboBox getCb_roomType() {
		return cb_roomType;
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AddCustomer frame = new AddCustomer();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AddCustomer() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 484, 651);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("姓 名:");
		lblNewLabel.setBounds(78, 48, 58, 15);
		contentPane.add(lblNewLabel);
		
		txt_name = new JTextField();
		txt_name.setBounds(174, 40, 128, 23);
		contentPane.add(txt_name);
		txt_name.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("姓 别:");
		lblNewLabel_1.setBounds(78, 94, 58, 15);
		contentPane.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("入户类型");
		lblNewLabel_2.setBounds(78, 183, 58, 15);
		contentPane.add(lblNewLabel_2);
		
		cb_roomType = new JComboBox();
		cb_roomType.setBounds(174, 179, 128, 22);
		contentPane.add(cb_roomType);
		
		JLabel lblNewLabel_2_2 = new JLabel("其中房费");
		lblNewLabel_2_2.setBounds(78, 234, 58, 15);
		contentPane.add(lblNewLabel_2_2);
		
		JLabel lblNewLabel_2_4 = new JLabel("会员卡级别");
		lblNewLabel_2_4.setBounds(78, 287, 74, 18);
		contentPane.add(lblNewLabel_2_4);
		
		JLabel lblNewLabel_2_5 = new JLabel("地址");
		lblNewLabel_2_5.setBounds(78, 330, 74, 23);
		contentPane.add(lblNewLabel_2_5);
		
		JLabel lblNewLabel_2_6 = new JLabel("入住时间");
		lblNewLabel_2_6.setBounds(78, 374, 58, 15);
		contentPane.add(lblNewLabel_2_6);
		
		JLabel lblNewLabel_2_7 = new JLabel("联系电话");
		lblNewLabel_2_7.setBounds(78, 457, 58, 15);
		contentPane.add(lblNewLabel_2_7);
		
		JLabel lblNewLabel_2_8 = new JLabel("预计离店时间");
		lblNewLabel_2_8.setBounds(78, 415, 74, 23);
		contentPane.add(lblNewLabel_2_8);
		
		text_charge = new JTextField();
		text_charge.setColumns(10);
		text_charge.setBounds(174, 230, 128, 23);
		contentPane.add(text_charge);
		
		vip_level = new JComboBox();
		vip_level.setBounds(174, 283, 128, 22);
		contentPane.add(vip_level);
		
		dp1 = new DatePicker();
		dp1.setBounds(174, 372, 128, 19);
		contentPane.add(dp1);
		
		JLabel lblNewLabel_3 = new JLabel("身份证号:");
		lblNewLabel_3.setBounds(78, 143, 58, 15);
		contentPane.add(lblNewLabel_3);
		
		txt_phone = new JTextField();
		txt_phone.setBounds(174, 454, 128, 21);
		contentPane.add(txt_phone);
		txt_phone.setColumns(10);
		
		JLabel lblNewLabel_4 = new JLabel("备注");
		lblNewLabel_4.setBounds(78, 514, 58, 21);
		contentPane.add(lblNewLabel_4);
		
		txt_note = new JTextField();
		txt_note.setBounds(174, 514, 128, 21);
		contentPane.add(txt_note);
		txt_note.setColumns(10);
		
		txt_address = new JTextField();
		txt_address.setColumns(10);
		txt_address.setBounds(174, 332, 128, 19);
		contentPane.add(txt_address);
		
		text_idCard = new JTextField();
		text_idCard.setColumns(10);
		text_idCard.setBounds(174, 140, 128, 23);
		contentPane.add(text_idCard);
		
		dp2 = new DatePicker();
		dp2.setBounds(174, 416, 128, 21);
		contentPane.add(dp2);
		
		radio_boy = new JRadioButton("男");
		buttonGroup.add(radio_boy);
		radio_boy.setBounds(175, 90, 58, 19);
		contentPane.add(radio_boy);
		
		radio_gril = new JRadioButton("女");
		buttonGroup.add(radio_gril);
		radio_gril.setBounds(235, 90, 62, 19);
		contentPane.add(radio_gril);
		JButton btn_saveCustomer = new JButton("保存客户信息");
		btn_saveCustomer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Customer c =add();
				if(validation()) {
					if(customerService.saveCustomer(c)) {
						ShowMessage.frameMessage(AddCustomer.this, true, "成功保存客人信息");
						dispose();
					}else {
						ShowMessage.frameMessage(AddCustomer.this, false, "保存失败");
					}
				}
			}
		});
		btn_saveCustomer.setBounds(78, 558, 116, 35);
		contentPane.add(btn_saveCustomer);
		
		JButton btnNewButton_1 = new JButton("取消");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		btnNewButton_1.setBounds(243, 558, 105, 35);
		contentPane.add(btnNewButton_1);
		
		JLabel lblNewLabel_5 = new JLabel("必填*");
		lblNewLabel_5.setFont(new Font("宋体", Font.PLAIN, 12));
		lblNewLabel_5.setBounds(325, 48, 58, 15);
		contentPane.add(lblNewLabel_5);
		
		JLabel lblNewLabel_5_1 = new JLabel("必填*");
		lblNewLabel_5_1.setFont(new Font("宋体", Font.PLAIN, 12));
		lblNewLabel_5_1.setBounds(325, 94, 58, 15);
		contentPane.add(lblNewLabel_5_1);
		
		JLabel lblNewLabel_5_2 = new JLabel("必填*");
		lblNewLabel_5_2.setFont(new Font("宋体", Font.PLAIN, 12));
		lblNewLabel_5_2.setBounds(325, 143, 58, 15);
		contentPane.add(lblNewLabel_5_2);
		
		JLabel lblNewLabel_5_3 = new JLabel("必填*");
		lblNewLabel_5_3.setFont(new Font("宋体", Font.PLAIN, 12));
		lblNewLabel_5_3.setBounds(325, 374, 58, 15);
		contentPane.add(lblNewLabel_5_3);
		this.setTitle("客人信息");
		inintCombox();
	}
	public void inintCombox() {
		DictionaryServiceImpl dsi = new DictionaryServiceImpl();
		List<Dictionary> roomType = dsi.findDictionaryByTypeID(TypeID.房间类型);
		List<Dictionary> vipLevelList = dsi.findDictionaryByTypeID(TypeID.会员级别);
		Dictionary d = new Dictionary();
		d.setNo(-1);
		d.setName("-----请选择----");
		d.setType_id(TypeID.默认.getId());
		roomType.add(0,d);
		vipLevelList.add(0,d);
		Dictionary[] type_room = roomType.toArray(new Dictionary[] {});
		Dictionary[] vip_dic = vipLevelList.toArray(new Dictionary[] {});
		DefaultComboBoxModel<Dictionary> roomDic = new DefaultComboBoxModel<Dictionary>(type_room);
		DefaultComboBoxModel<Dictionary> vipDic = new DefaultComboBoxModel<Dictionary>(vip_dic);
		cb_roomType.setModel(roomDic);
		vip_level.setModel(vipDic);
		
	}
	/**
	 * 客人信息添加方法
	 * @return
	 */
	public Customer add() {
		Customer c = new Customer();
		c.setName(txt_name.getText());
		c.setAddress(txt_address.getText());
		try {
			Date d = CastUtils.parse(dp1.getInnerTextField().getText());
			c.setCome_date(CastUtils.utilDateToSqlTimestamp(d));
			Date d2 = CastUtils.parse(dp2.getInnerTextField().getText());
			c.setLeave_date(CastUtils.utilDateToSqlTimestamp(d2));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Object obj = vip_level.getSelectedItem();
		Dictionary d = (Dictionary) obj;
		c.setCard(d.getNo());
		if(radio_boy.isSelected()) {
			c.setGender("男");
		}else {
			c.setGender("女");
		}
		c.setNote(txt_note.getText());
		c.setPhone_num(CastUtils.castInt(txt_phone.getText()));
		c.setRoom_price(CastUtils.castDouble(text_charge.getText()));
		Object obj2 = cb_roomType.getSelectedItem();
		Dictionary d2 = (Dictionary)obj2;
		c.setRoom_type(d2.getNo());
		c.setId(CastUtils.castInt(text_idCard.getText()));
		if(d.getNo()>0) {
			c.setVip_status(1);//1代表是会员0代表普通顾客
		}else {
			c.setVip_status(0);
		}
		return c;	
	}
	public  boolean validation() {
		if(txt_name.getText().equals("")) {
			ShowMessage.frameMessage(AddCustomer.this, false,"用户名不能为空");
			return false;
		}else if((!radio_boy.isSelected())&&(!radio_gril.isSelected())){
			ShowMessage.frameMessage(AddCustomer.this, false,"性别不能为空");
			return false;
		}else if(text_idCard.getText().equals("")) {
			ShowMessage.frameMessage(AddCustomer.this, false,"身份证号不能为空");
			return false;
		}else if(dp1.getInnerTextField().getText().equals("")) {
			ShowMessage.frameMessage(AddCustomer.this, false, "入住时间不能为空");
			return false;
		}else {
			return true;
		}
	}
}
